package footprint.kwlee.app.detail;

import footprint.kwlee.app.R;
import footprint.kwlee.app.db.Fp_DatabaseHelper;
import footprint.kwlee.app.db.Fp_Database_sql;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import footprint.kwlee.app.db.*;

public class Detail_content extends Activity implements View.OnClickListener {

	private Intent detail_edit_intent;
	private Fp_DatabaseHelper Fp_DatabaseHelper = null;
	private SQLiteDatabase db = null;
	private Button list_button;
	private Button edit_button;
	private TextView tv_date;
	private TextView tv_time;
	private TextView tv_kind;
	private ImageView imv1;
	private ImageView imv2;
	private TextView tv_memo;
	private TextView tv_addr;
	private Intent my_intent;
	private String idx;
	private Cursor cursor = null;
	private DetailItem detail_item;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		detail_edit_intent = new Intent(this, DetailEdit.class);

		setContentView(R.layout.detail_content_layout);

		Fp_DatabaseHelper = new Fp_DatabaseHelper(this.getApplicationContext());
		db = Fp_DatabaseHelper.getWritableDatabase();

		list_button = (Button) findViewById(R.id.Button11_01);
		edit_button = (Button) findViewById(R.id.Button11_02);

		tv_date = (TextView) findViewById(R.id.TextView11_02);
		tv_time = (TextView) findViewById(R.id.TextView11_05);
		tv_kind = (TextView) findViewById(R.id.TextView11_03);
		tv_memo = (TextView) findViewById(R.id.TextView11_01);
		tv_addr = (TextView) findViewById(R.id.TextView11_04);

		imv1 = (ImageView) findViewById(R.id.ImageView11_01);
		imv2 = (ImageView) findViewById(R.id.ImageView11_02);

		list_button.setOnClickListener(this);
		edit_button.setOnClickListener(this);
		imv1.setOnClickListener(this);
		imv2.setOnClickListener(this);
	}

	@Override
	public void onStart() {
		super.onStart();

	}

	public void onResume() {
		super.onResume();
		my_intent = getIntent();
		idx = my_intent.getStringExtra("id");
		cursor = db.query(Fp_Database_sql.DETAILFOOTPRINT, null, "_id=?",
				new String[] { idx }, null, null, null);

		startManagingCursor(cursor);

		if (cursor.moveToFirst()) {
			if (detail_item == null) {
				detail_item = new DetailItem();
			}
			detail_item.setD_id(cursor.getInt(0));
			detail_item.setBigfootprint_id(cursor.getInt(1));
			detail_item.setDetail_date(cursor.getString(2));
			detail_item.setDetail_time(cursor.getString(3));
			detail_item.setDetail_pic1_path(cursor.getString(4));
			detail_item.setDetail_pic2_path(cursor.getString(5));
			detail_item.setDetail_pic3_path(cursor.getString(6));
			detail_item.setPhoto1(cursor.getString(4));
			detail_item.setPhoto2(cursor.getString(5));
			detail_item.setDetail_memo(cursor.getString(7));
			detail_item.setDetail_addr(cursor.getString(8));
			detail_item.setMember_pwd(cursor.getString(9));
			detail_item.setDetail_kind(cursor.getString(10));
			detail_item.setLatitude(cursor.getString(11));
			detail_item.setLongitude(cursor.getString(12));
			
			tv_date.setText(detail_item.getDetail_date());
			tv_time.setText(detail_item.getDetail_time());
			tv_kind.setText(detail_item.getDetail_kind());
			tv_memo.setText(detail_item.getDetail_memo());
			tv_addr.setText(detail_item.getDetail_addr());

			if (detail_item.getPhoto1() != null)
				imv1.setImageDrawable(detail_item.getPhoto1());
			else
				imv1.setImageResource(R.drawable.noimage);
			if (detail_item.getPhoto2() != null)
				imv2.setImageDrawable(detail_item.getPhoto2());
			else
				imv2.setImageResource(R.drawable.noimage);
		}

	}

	public void onClick(View v) {
		if (v == list_button) {
			setResult(RESULT_OK);
			finish();
		} else if (v == edit_button) {
			detail_edit_intent.putExtra("id", idx);
			startActivityForResult(detail_edit_intent, 0);
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {

			} else {

			}
		}
	}

}
